package top.lvkun.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lvkun.basic.utils.AjaxResult;
import top.lvkun.basic.utils.PageList;
import top.lvkun.org.domain.Shop;
import top.lvkun.org.query.ShopQuery;
import top.lvkun.org.service.IShopService;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "门店接口",description = "门店接口详细描述")
public class ShopController {
    @Autowired
    IShopService shopService;

    @GetMapping("{id}")
    @ApiOperation(value = "通过门店id查询",notes = "通过门店id查询")
    public Shop getById(@PathVariable("id") Long id){
        return shopService.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "门店添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            System.out.println(shop);
            if (shop.getId() == null) {
                shopService.add(shop);
            }else {
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作繁忙！"+e.getMessage());
        }
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "删除数据",notes = "通过门店id删除数据")
    public AjaxResult delById(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作繁忙！"+e.getMessage());
        }
    }

    //查询所有
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "查询所有")
    public List<Shop> getAll(){
        return shopService.getAll();
    }
    
    //分页+高级查询
    @PostMapping
    @ApiOperation(value = "分页+高级查询",notes = "参数ShopQuery")
    public PageList<Shop> getPage(@RequestBody ShopQuery query){
        return shopService.queryData(query);
    }

    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "参数ids")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            shopService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作繁忙！"+e.getMessage());
        }
    }

    @GetMapping("/{id}/{type}")
    public AjaxResult shopAudit(@PathVariable("id") Long id,
                                @PathVariable("type") Integer type){
        try {

            shopService.shopAudit(id,type);
            return AjaxResult.me().setMessage("审核通过！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("审核提交失败！请重试！");
        }


    }


}
